/*
 
 Opgave 1
 
 forback.c
 
 */

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>

#include "forback.h"

int status;

/* start the program specified by filename with the arguments in argv 
 in a new process and wait for termination */

int foregroundcmd(char *filename, char *argv[]) {
	pid_t pid = fork();
	
	//fork() returns 0 if it's the child process.
	if(pid == 0) {
		//executing the commands
		execvp(filename, argv);
	}else {
		//Printing
		printf("waiting for termination (foreground) \n");
		//Finding status for the child process, and waiting for termination
		waitpid(pid,&status,0);	
		printf("terminated (foreground)\n");
	}
}

/* start the program specified by filename with the arguments in argv 
   in a new process */
int backgroundcmd(char *filename, char *argv[])
{
  	pid_t pid = fork();
	
	//fork() returns 0 if it's the child process.
	if(pid == 0) {
		//executing the commands
		execvp(filename, argv);
	}else {
		//Printing
		printf("background started \n");
		//returning
		return 0;
	}
}

